<?php

namespace App\Http\Controllers;

use App\Train;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\storage;

class TrainController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $trains = Train::latest('created_at')->paginate(8);
        return view('Train.train' , compact('trains'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('Train.createTrain');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $datas=[
        'train_name'=>'required',
         'train_picture'=>'required',
         'train_intro'=>'required',
         'train_rules'=>'required',
         'train_date'=>'required',
         'train_address'=>'required',
         'counts'=>'required',
         'train_price'=>'required',
         'train_classify'=>'required',
        ];
        $this->validate($request,$datas);
        $this->uploadPicture($request);
        $trainPic=$request->get('train_name').'.'.$request->file('train_picture')->getClientOriginalExtension();
        $trains=new Train;
        $trains->train_name=$request->get('train_name');
        $trains->train_picture=$trainPic;
        $trains->train_intro=$request->get('train_intro');
        $trains->train_rules=$request->get('train_rules');
        $trains->train_date=$request->get('train_date');
        $trains->train_address=$request->get('train_address');
        $trains->counts=$request->get('counts');
        $trains->train_price=$request->get('train_price');
        $trains->train_classify=$request->get('train_classify');
        $trains->save();

        session()->flash('success','培训创建成功！');
        return redirect()->route('train.index');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $trains = Train::findOrFail($id);
        return view('Train.editTrain',compact('trains'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $trains=Train::findOrFail($id);
        if($request->file('train_picture')){
            $Picture=$trains->train_picture;
            if($Picture!='default.jpg'){
                Storage::disk('uploads')->exists($Picture);
                Storage::disk('uploads')->delete($Picture);
            }
            $this->uploadPicture($request);
            $trains->train_picture=$request->get('train_name'). '.' . $request->file('train_picture')->getClientOriginalExtension();
            $trains->save();
        }
        $trains->update([
            $trains->train_name=$request->get('train_name'),
            $trains->train_intro=$request->get('train_intro'),
            $trains->train_rules=$request->get('train_rules'),
            $trains->train_date=$request->get('train_date'),
            $trains->train_address=$request->get('train_address'),
            $trains->counts=$request->get('counts'),
            $trains->train_price=$request->get('train_price'),
            $trains->train_classify=$request->get('train_classify')
        ]);
        $trains->save();
        session()->flash('success','更新活动成功');
        return redirect()->route('train.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $train = Train::findOrFail($id);
        $Picture=$train->train_picture;
        if($Picture!='default.jpg'){
            Storage::disk('uploads')->exists($Picture);
            Storage::disk('uploads')->exists($Picture);
        }
        $train->delete();
        session()->flash('success', '删除培训成功');
        return redirect()->route('train.index');
    }
    public function uploadPicture(Request $request){
        if($request->isMethod('post') OR $request->isMethod('patch')){
            $Picture=$request->file('train_picture');
            if($Picture->isValid()){
                $realPath=$Picture->getRealPath();
                $ext=$Picture->getClientOriginalExtension();
                $trainName=$request->get('train_name');
                $fileName=$request->get('train_name').'.'.$ext;
                Image::make($Picture)->fit(300,300)->save(public_path('/uploads/avatars/').$fileName);
            }
        }
    }
}
